﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using EzDesk.MVVMBase;
using EzDesk.DesktopClient.ViewContract;
using EzDesk.Client.Utility;
using System.Windows;
using EzDesk.Generic.WPF;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.MVVMEnhance;
using EzDesk.DesktopClient.ServiceLocator.ClientService;

namespace EzDesk.DesktopClient.ViewModel
{
    public class AppItemViewModel : ViewModelWithSingleContainer<IAppItemView>
    {
        private AppManagement _appManagement;

        private App _appModel;
        public App AppModel
        {
            get {
                return _appModel; }
            set
            {
                _appModel = value;
                RaisePropertyChanged("AppModel");
            }
        }

        private ObservableCollection<TaskItemViewModel> _taskItemViewModelCollection;

        public ObservableCollection<TaskItemViewModel> TaskItemViewModelCollection
        {
            get
            {
                if (_taskItemViewModelCollection == null)
                {
                    _taskItemViewModelCollection = new ObservableCollection<TaskItemViewModel>(
                        _appModel
                        .GetTasks()
                        .Select(task => new TaskItemViewModel(task)));
                        
                }
                return _taskItemViewModelCollection;
            }
            set { _taskItemViewModelCollection = value; }
        }

        public AppItemViewModel()
            : base()
        {
            Init();
        }
        public AppItemViewModel(App app)
        {
            this._appModel = app;
            Init();
        }

        private void Init()
        {
            InitManagement();
            InitEvent();
        }

        private void InitEvent()
        {
            this.View.OpenApp += new EventHandler(View_OpenApp);
        }

        void View_OpenApp(object sender, EventArgs e)
        {
            FunctionNavigateEvent.Instance.Publish(new FunctionNavigateArgs { Url = this.AppModel.Domain});
        }

        private void InitManagement()
        {
            _appManagement = this.Container.Resolve<AppManagement>();
        }
    }
}
